package com.chuangjiangx.member.manager.basic.web.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

/**
 * Created by 陈智 on 2018/6/13.
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {

    @Value("${spring.application.name}")
    public String appName;
    @Value("${spring.profiles.active:prod}")
    public String active;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //组名
                .groupName("com.chuangjiangx." + appName)
//                .enable(!SpringProfile.prod.equals(SpringProfile.valueOf(active)))
                //关闭默认的响应Code
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, responseMessageList())
                .globalResponseMessage(RequestMethod.POST, responseMessageList())
                .globalResponseMessage(RequestMethod.PUT, responseMessageList())
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList())
                .enableUrlTemplating(true)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.chuangjiangx.mbrmanager.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * API信息
     */
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(appName + "接口文档")
                .version("1.0")
                .build();
    }

    /**
     * 通用的response
     */
    @Bean
    public List<ResponseMessage> responseMessageList() {
        ResponseMessage m500 = new ResponseMessageBuilder()
                .code(500)
                .message("服务器开小差了")
                .build();
        ResponseMessage m400 = new ResponseMessageBuilder()
                .code(400)
                .message("您的请求有误,is bad request!")
                .build();
        ResponseMessage m403 = new ResponseMessageBuilder()
                .code(403)
                .message("您无权限访问")
                .build();
        ResponseMessage m404 = new ResponseMessageBuilder()
                .code(404)
                .message("您请求的资源不存在")
                .build();
        return Arrays.asList(m400, m403, m404, m500);
    }
}
